<template>
  <div>
    <el-card style="width: 500px;">
    <el-form status-icon :model="form" :rules="rules" ref="passForm">
      <el-form-item size="medium" prop="password" label="原密码" label-width="100px">
        <el-input size="medium" placeholder="请输入原密码" prefix-icon="el-icon-lock" show-password
                  v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item size="medium" prop="newPassword" label="新密码" label-width="100px">
        <el-input size="medium" placeholder="请输入新密码" prefix-icon="el-icon-lock" show-password
                  v-model="form.newPassword"></el-input>
      </el-form-item>
      <el-form-item size="medium" prop="newPassword2" label="确认新密码" label-width="100px">
        <el-input size="medium" placeholder="请再次输入新密码" prefix-icon="el-icon-lock"
                  show-password
                  v-model="form.newPassword2"></el-input>
      </el-form-item>
      <el-form-item style="margin: 10px 0; text-align: center">
        <el-button type="primary" size="medium" style="width: 25%" autocomplete="off" @click="modify">确定修改</el-button>
      </el-form-item>
    </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "ModifyPass",
  data(){
    let validatePassword=(rule,value,callback)=>{
      if(value===''){
        callback(new Error('请输入原密码'));
      }else if(value!==this.user.password){
        callback(new Error('密码错误'))
      }else{
        callback()
      }
    };
    let validateNewPassword=(rule,value,callback)=>{
      if(value===''){
        callback(new Error('请输入新密码'));
      }else{
        if(this.form.newPassword2 !==''){
          this.$refs.passForm.validateField('newPassword2');
        }
        callback();
      }
    };
    let validateNewPassword2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      form:{
        password:'',
        newPassword:'',
        newPassword2:''
      },
      rules: {
        password: [
          {validator:validatePassword, trigger: 'blur'}
        ],
        newPassword: [
          {validator:validateNewPassword, trigger: 'blur'}
        ],
        newPassword2: [
          {validator:validateNewPassword2, trigger: ['blur','change']}
        ],
      },
      user:localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")):{}
    }
  },
  methods:{
    async getUser(){
      return (await this.request.get("/user/username/"+this.user.username)).data
    },
    modify(){
      this.$refs['passForm'].validate((valid) => {
        if (valid) { //表单校验合法
          this.user.password=this.form.newPassword
          this.request.post("/user",this.user).then(res =>{
            if(res.code==='200'){
              this.$message.success("修改密码成功")
              localStorage.setItem("user",JSON.stringify(this.user))
              this.form={}
              // //更新浏览器存储的用户信息
              // this.getUser().then(res=>{
              //   res.token=JSON.parse(localStorage.getItem("user")).token;
              //   localStorage.setItem("user",JSON.stringify(res))
              // })

            }else{
              this.$message.error("修改失败")
            }
          })
        }
      });
    }
  }
}
</script>

<style scoped>

</style>